package com.mijie.homi.dao.user;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.mijie.core.IBatisDao;
import com.mijie.homi.common.domain.user.User;

@Repository
@Transactional
public class UserDao extends IBatisDao{

	public int emailVerifyPass(int id){
		return this.update("emailVerifyPass", id);
	}
	
	public User getUserById(int id){
		return this.selectOne("getUserById", id);
	}
	
	public int settingAvatar(int id,String avatar){
		HashMap<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("id", id);
		parameter.put("avatar", avatar);
		return this.update("settingAvatar", parameter);
	}
	
	public int settingPasswd(int id,String passwd){
		HashMap<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("id", id);
		parameter.put("passwd", passwd);
		return this.update("settingPasswd", parameter);
	}
	
	public User getUserByEmail(String email){
		return this.selectOne("getUserByEmail", email);
	}
	
	public List<User> listUserByIds(int[] array){
		return this.selectList("listUserByIds", array);
	}
	
	public int register(User user){
		return this.insert("register", user);
	}
	
	public int update(User user){
		return this.update("update",user);
	}
	
	@Transactional(rollbackFor={Exception.class})
	public int updateGender(int id,int gender) throws Exception{
		HashMap<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("id", id);
		parameter.put("gender", gender);
		int rows =  this.update("updateGender1",parameter);
		if(rows>0){
			rows = this.update("updateGender2",parameter);
			if(rows==0) throw new Exception("Can't found userExtInfo with userId:"+id);
		}
		return rows;
	}
	
	public int updateRegion(int id,String nation,String province,String city,String district){
		HashMap<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("id", id);
		parameter.put("nation", nation);
		parameter.put("province", province);
		parameter.put("city", city);
		parameter.put("district", district);
		return this.update("updateRegion",parameter);
	}
	
	public int updateNickName(int id,String nickName){
		HashMap<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("id", id);
		parameter.put("nickName", nickName);
		return this.update("updateNickName",parameter);
	}
	
	public int updateName(int id,String name){
		HashMap<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("id", id);
		parameter.put("name", name);
		return this.update("updateName",parameter);
	}
	
	public boolean isContact(String uuid){
		String tmp = this.selectStr("isContact",uuid);
		return tmp!=null;
	}
	
	public int createInternalUser(List<User> list){
		int rows =  this.insert("createInternalUser", list);
		return rows;
	}
	
	public List<Integer> listInternalUser(){
		return this.selectIntList("listInternalUser");
	}
	
	public List<Integer> listUserInfoSubcribers(int userId){
		return this.selectIntList("listUserInfoSubcribers",userId);
	}
	
	public int addUserInfoSubcribe(int subscriber,int targetId){
		HashMap<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("subscriber", subscriber);
		parameter.put("targetId", targetId);
		return this.insert("addUserInfoSubcribe", parameter);
	}
	
	public int deleteUserInfoSubcribe(int subscriber,int targetId){
		HashMap<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("subscriber", subscriber);
		parameter.put("targetId", targetId);
		return this.delete("deleteUserInfoSubcribe", parameter);
	}
	
	public int updateEmailByUserId(Map<String, Object> param){
		return this.update("updateEmailByUserId", param);
	}
}
